package com.cheer.ba.presenter.impl;

import android.content.Context;

import com.cheer.ba.R;
import com.cheer.ba.cache.preference.Session;
import com.cheer.ba.constant.CodeStatus;
import com.cheer.ba.constant.ConstantTag;
import com.cheer.ba.constant.Constants;
import com.cheer.ba.constant.ReturnCode;
import com.cheer.ba.http.BaseCallback;
import com.cheer.ba.http.CallManager;
import com.cheer.ba.http.HandleRetCode;
import com.cheer.ba.http.Http;
import com.cheer.ba.http.request.APIService;
import com.cheer.ba.model.LookMyBillModel;
import com.cheer.ba.model.MyOrderModel;
import com.cheer.ba.model.MyTradeRecordModel;
import com.cheer.ba.model.PointModel;
import com.cheer.ba.model.ResponseData;
import com.cheer.ba.presenter.GeneralPresenter;
import com.cheer.ba.utils.NetUtils;
import com.cheer.ba.view.base.IViewController;

import java.util.List;

import retrofit2.Call;
import retrofit2.Response;

/**
 * 违章查询处理
 *
 * @author HQ
 *         created at 2017/3/3 0003 15:50
 */
public class CloudBookPresenter extends GeneralPresenter {

    private Context mContext;
    private IViewController mView;
    private APIService mUserService;

    //记录当前分页下标
    private int mRecordPage = 0;
    private int mIndex = 0;

    //数据是否加载失败
    private boolean isDataError = false;

    @Override
    public void attachView(IViewController view) {
        this.mView = view;
        mContext = getContext(view);
        mUserService = Http.getUserService(mContext);
    }

    @Override
    public void loadData(boolean loadMore) {
    }

    /**
     * 搜索我的账本
     *
     * @param loadMore
     * @param logType   0拿所有 1进 2出
     * @param startDate
     * @param endDate
     */
    public void searchMyBooks(boolean isShowLoad, final boolean loadMore, String logType, String startDate, String endDate) {
        if (!NetUtils.isNetworkConnected(mContext)) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_NET, CodeStatus.Gegeneral.ERROR_NET, "", loadMore);
            return;
        }
        if (!loadMore) {
            isDataError = false;
            mRecordPage = 0;
        } else {
            if (isDataError) {
                mRecordPage = mIndex;
            } else {
                mRecordPage += Constants.PAGESIZE;
            }
        }
        mIndex = mRecordPage;
        if (isShowLoad) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_LOADING, CodeStatus.Gegeneral.DATA_LOADING, "", loadMore);
        }
        Call<ResponseData<List<LookMyBillModel>>> call = mUserService.searchMyBooks(1508, Session.getUserId(), logType, startDate, endDate, mRecordPage, Constants.PAGESIZE);
        CallManager.add(call);
        call.enqueue(new BaseCallback<ResponseData<List<LookMyBillModel>>>() {
            @Override
            public void onResponse(Call<ResponseData<List<LookMyBillModel>>> call, Response<ResponseData<List<LookMyBillModel>>> response) {
                super.onResponse(call, response);
                if (response.isSuccessful()) {
                    ResponseData<List<LookMyBillModel>> res = response.body();
                    if (res.getRet() == ReturnCode.SUCCESS) {
                        List<LookMyBillModel> books = res.getData();
                        if (null != books) {
                            if (books.isEmpty()) {
                                isDataError = true;
                                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_SUCCESS_NULL, CodeStatus.Gegeneral.DATA_SUCCESS_NULL, "", loadMore);
                            } else {
                                isDataError = false;
                                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, "", loadMore);
                            }
                            mView.updateViewWithLoadMore(books, loadMore, ConstantTag.SEARCH_MY_BOOK);
                            return;
                        }
                    }
                }
                isDataError = true;
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "", loadMore);
            }

            @Override
            public void onFailure(Call<ResponseData<List<LookMyBillModel>>> call, Throwable t) {
                super.onFailure(call, t);
                isDataError = true;
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "", loadMore);
            }
        });
    }

    /**
     * 移除账本
     */
    public void delMyBooks(String bookId) {
        if (!NetUtils.isNetworkConnected(mContext)) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_NET, CodeStatus.Gegeneral.ERROR_NET, mContext.getString(R.string.no_net_work));
            return;
        }
        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_LOADING, CodeStatus.Gegeneral.DATA_LOADING, mContext.getString(R.string.data_loading));
        Call<ResponseData<LookMyBillModel>> call = mUserService.delMyBooks(1002, Session.getUserId(), bookId);
        CallManager.add(call);
        call.enqueue(new BaseCallback<ResponseData<LookMyBillModel>>() {
            @Override
            public void onResponse(Call<ResponseData<LookMyBillModel>> call, Response<ResponseData<LookMyBillModel>> response) {
                super.onResponse(call, response);
                if (response.isSuccessful()) {
                    ResponseData<LookMyBillModel> res = response.body();
                    if (res.getRet() == ReturnCode.SUCCESS) {
                        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, "");
                        mView.updateViewWithLoadMore(ReturnCode.SUCCESS, false, ConstantTag.SUGGEST_SUCCESS);
                        return;
                    } else {
                        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "");
                        HandleRetCode.handler(mContext, res, true, "移除失败");
                        return;
                    }
                }
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "移除失败");
            }

            @Override
            public void onFailure(Call<ResponseData<LookMyBillModel>> call, Throwable t) {
                super.onFailure(call, t);
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "移除失败");
            }
        });
    }

    /**
     * 获得我的交易记录
     *
     * @param loadMore
     */
    public void getMyTrades(final boolean isShowLoad, final boolean loadMore) {
        if (!NetUtils.isNetworkConnected(mContext)) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_NET, CodeStatus.Gegeneral.ERROR_NET, "", loadMore);
            return;
        }
        if (!loadMore) {
            isDataError = false;
            mRecordPage = 0;
        } else {
            if (isDataError) {
                mRecordPage = mIndex;
            } else {
                mRecordPage += Constants.PAGESIZE;
            }
        }
        mIndex = mRecordPage;
        if (isShowLoad) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_LOADING, CodeStatus.Gegeneral.DATA_LOADING, "", loadMore);
        }
        Call<ResponseData<List<LookMyBillModel>>> call = mUserService.getMyTrades(1508, Session.getUserId(), "0", mRecordPage, Constants.PAGESIZE);
        CallManager.add(call);
        call.enqueue(new BaseCallback<ResponseData<List<LookMyBillModel>>>() {
            @Override
            public void onResponse(Call<ResponseData<List<LookMyBillModel>>> call, Response<ResponseData<List<LookMyBillModel>>> response) {
                super.onResponse(call, response);
                if (response.isSuccessful()) {
                    ResponseData<List<LookMyBillModel>> res = response.body();
                    if (res.getRet() == ReturnCode.SUCCESS) {
                        List<LookMyBillModel> articles = res.getData();
                        if (null != articles) {
                            if (articles.isEmpty()) {
                                isDataError = true;
                                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_SUCCESS_NULL, CodeStatus.Gegeneral.DATA_SUCCESS_NULL, "", loadMore);
                            } else {
                                isDataError = false;
                                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, "", loadMore);
                            }
                            mView.updateViewWithLoadMore(articles, loadMore, ConstantTag.GET_MY_TRADE);
                            return;
                        }
                    }
                }
                isDataError = true;
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "", loadMore);
            }

            @Override
            public void onFailure(Call<ResponseData<List<LookMyBillModel>>> call, Throwable t) {
                super.onFailure(call, t);
                isDataError = true;
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "", loadMore);
            }
        });
    }

    /**
     * 油卡变更记录
     *
     * @param loadMore
     */
    public void getMyDevice(final boolean isShowLoad, final boolean loadMore, String cardId) {
        if (!NetUtils.isNetworkConnected(mContext)) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_NET, CodeStatus.Gegeneral.ERROR_NET, "", loadMore);
            return;
        }
        if (!loadMore) {
            isDataError = false;
            mRecordPage = 0;
        } else {
            if (isDataError) {
                mRecordPage = mIndex;
            } else {
                mRecordPage += Constants.PAGESIZE;
            }
        }
        mIndex = mRecordPage;
        if (isShowLoad){
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_LOADING, CodeStatus.Gegeneral.DATA_LOADING, "", loadMore);
        }
        Call<ResponseData<List<LookMyBillModel>>> call = mUserService.changeOilCard(1508, cardId, "0", "", mRecordPage, Constants.PAGESIZE);
        CallManager.add(call);
        call.enqueue(new BaseCallback<ResponseData<List<LookMyBillModel>>>() {
            @Override
            public void onResponse(Call<ResponseData<List<LookMyBillModel>>> call, Response<ResponseData<List<LookMyBillModel>>> response) {
                super.onResponse(call, response);
                if (response.isSuccessful()) {
                    ResponseData<List<LookMyBillModel>> res = response.body();
                    if (res.getRet() == ReturnCode.SUCCESS) {
                        List<LookMyBillModel> articles = res.getData();
                        if (null != articles) {
                            if (articles.isEmpty()) {
                                isDataError = true;
                                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_SUCCESS_NULL, CodeStatus.Gegeneral.DATA_SUCCESS_NULL, "", loadMore);
                            } else {
                                isDataError = false;
                                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, "", loadMore);
                            }
                            mView.updateViewWithLoadMore(articles, loadMore, ConstantTag.GET_MY_TRADE);
                            return;
                        }
                    }
                }
                isDataError = true;
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "", loadMore);
            }

            @Override
            public void onFailure(Call<ResponseData<List<LookMyBillModel>>> call, Throwable t) {
                super.onFailure(call, t);
                isDataError = true;
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "", loadMore);
            }
        });
    }

    /**
     * 获取我的发票信息
     *
     * @param loadMore
     */
    public void searchMyInvoices(final boolean loadMore) {
        if (!NetUtils.isNetworkConnected(mContext)) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_NET, CodeStatus.Gegeneral.ERROR_NET, "", loadMore);
            return;
        }
        if (!loadMore) {
            isDataError = false;
            mRecordPage = 0;
        } else {
            if (isDataError) {
                mRecordPage = mIndex;
            } else {
                mRecordPage += Constants.PAGESIZE;
            }
        }
        mIndex = mRecordPage;
        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_LOADING, CodeStatus.Gegeneral.DATA_LOADING, "", loadMore);
        Call<ResponseData<List<MyOrderModel>>> call = mUserService.myInvoices(1508, Session.getUserId(), mRecordPage, Constants.PAGESIZE);
        CallManager.add(call);
        call.enqueue(new BaseCallback<ResponseData<List<MyOrderModel>>>() {
            @Override
            public void onResponse(Call<ResponseData<List<MyOrderModel>>> call, Response<ResponseData<List<MyOrderModel>>> response) {
                super.onResponse(call, response);
                if (response.isSuccessful()) {
                    ResponseData<List<MyOrderModel>> res = response.body();
                    if (res.getRet() == ReturnCode.SUCCESS) {
                        List<MyOrderModel> books = res.getData();
                        if (null != books) {
                            if (books.isEmpty()) {
                                isDataError = true;
                            } else {
                                isDataError = false;
                            }
                            mView.onLoadingStatus(mView, true, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, "", loadMore);
                            mView.updateViewWithLoadMore(books, loadMore, ConstantTag.GET_LIST_SUCCESS);
                            return;
                        }
                    }
                }
                isDataError = true;
                mView.onLoadingStatus(mView, true, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "", loadMore);
            }

            @Override
            public void onFailure(Call<ResponseData<List<MyOrderModel>>> call, Throwable t) {
                super.onFailure(call, t);
                isDataError = true;
                mView.onLoadingStatus(mView, true, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "", loadMore);
            }
        });
    }

    /**
     * 申请发票
     */
    public void askMyInvoice(String invoiceType, String payCost, String orderId, String invoiceNum, String invoiceTitle, String mBankName, String mBankNum, String name, String tel, String location) {
        if (!NetUtils.isNetworkConnected(mContext)) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_NET, CodeStatus.Gegeneral.ERROR_NET, mContext.getString(R.string.no_net_work));
            return;
        }
        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_LOADING, CodeStatus.Gegeneral.DATA_LOADING, "");
        Call<ResponseData<MyOrderModel>> call = mUserService.addMyInvoices(1002, Session.getUserId(), invoiceType, payCost, orderId, invoiceNum, invoiceTitle, mBankName, mBankNum, "", name, tel, location);
        CallManager.add(call);
        call.enqueue(new BaseCallback<ResponseData<MyOrderModel>>() {
            @Override
            public void onResponse(Call<ResponseData<MyOrderModel>> call, Response<ResponseData<MyOrderModel>> response) {
                super.onResponse(call, response);
                if (response.isSuccessful()) {
                    ResponseData<MyOrderModel> res = response.body();
                    if (res.getRet() == ReturnCode.SUCCESS) {
                        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, "");
                        mView.updateViewWithTag("发票申请成功", ConstantTag.DEFAULT_SUCCESS);
                        return;
                    } else {
                        HandleRetCode.handler(mContext, res, true, "申请失败");
                        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "");
                        return;
                    }
                }
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "申请失败");
            }

            @Override
            public void onFailure(Call<ResponseData<MyOrderModel>> call, Throwable t) {
                super.onFailure(call, t);
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "申请失败");
            }
        });
    }

    /**
     * 获取我的积分
     *
     * @param loadMore
     */
    public void getMyPoints(final boolean loadMore) {
        if (!NetUtils.isNetworkConnected(mContext)) {
            mView.onLoadingStatus(mView, true, CodeStatus.Gegeneral.ERROR_NET, CodeStatus.Gegeneral.ERROR_NET, "", loadMore);
            return;
        }
        if (!loadMore) {
            isDataError = false;
            mRecordPage = 0;
        } else {
            if (isDataError) {
                mRecordPage = mIndex;
            } else {
                mRecordPage += Constants.PAGESIZE;
            }
        }
        mIndex = mRecordPage;
        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_LOADING, CodeStatus.Gegeneral.DATA_LOADING, "", loadMore);
        Call<ResponseData<List<PointModel>>> call = mUserService.getMyPoints(1508, Session.getUserId(), "0", mRecordPage, Constants.PAGESIZE);
        CallManager.add(call);
        call.enqueue(new BaseCallback<ResponseData<List<PointModel>>>() {
            @Override
            public void onResponse(Call<ResponseData<List<PointModel>>> call, Response<ResponseData<List<PointModel>>> response) {
                super.onResponse(call, response);
                if (response.isSuccessful()) {
                    ResponseData<List<PointModel>> res = response.body();
                    if (res.getRet() == ReturnCode.SUCCESS) {
                        List<PointModel> books = res.getData();
                        if (null != books) {
                            if (books.isEmpty()) {
                                isDataError = true;
                            } else {
                                isDataError = false;
                            }
                            mView.onLoadingStatus(mView, true, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, "", loadMore);
                            mView.updateViewWithLoadMore(books, loadMore, ConstantTag.GET_LIST_SUCCESS);
                            return;
                        }
                    }
                }
                isDataError = true;
                mView.onLoadingStatus(mView, true, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "", loadMore);
            }

            @Override
            public void onFailure(Call<ResponseData<List<PointModel>>> call, Throwable t) {
                super.onFailure(call, t);
                isDataError = true;
                mView.onLoadingStatus(mView, true, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "", loadMore);
            }
        });
    }

    /**
     * 获得我的提现记录
     *
     * @param loadMore
     */
    public void getMoneyRecords(final boolean isShowLoad, final boolean loadMore) {
        if (!NetUtils.isNetworkConnected(mContext)) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_NET, CodeStatus.Gegeneral.ERROR_NET, "", loadMore);
            return;
        }
        if (!loadMore) {
            isDataError = false;
            mRecordPage = 0;
        } else {
            if (isDataError) {
                mRecordPage = mIndex;
            } else {
                mRecordPage += Constants.PAGESIZE;
            }
        }
        mIndex = mRecordPage;
        if (isShowLoad) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_LOADING, CodeStatus.Gegeneral.DATA_LOADING, "", loadMore);
        }
        Call<ResponseData<List<LookMyBillModel>>> call = mUserService.getMoneyRecords(1508, Session.getUserId(), mRecordPage, Constants.PAGESIZE);
        CallManager.add(call);
        call.enqueue(new BaseCallback<ResponseData<List<LookMyBillModel>>>() {
            @Override
            public void onResponse(Call<ResponseData<List<LookMyBillModel>>> call, Response<ResponseData<List<LookMyBillModel>>> response) {
                super.onResponse(call, response);
                if (response.isSuccessful()) {
                    ResponseData<List<LookMyBillModel>> res = response.body();
                    if (res.getRet() == ReturnCode.SUCCESS) {
                        List<LookMyBillModel> articles = res.getData();
                        if (null != articles) {
                            if (articles.isEmpty()) {
                                isDataError = true;
                                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_SUCCESS_NULL, CodeStatus.Gegeneral.DATA_SUCCESS_NULL, "", loadMore);
                            } else {
                                isDataError = false;
                                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, "", loadMore);
                            }
                            mView.updateViewWithLoadMore(articles, loadMore, ConstantTag.GET_MY_TRADE);
                            return;
                        }
                    }
                }
                isDataError = true;
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "", loadMore);
            }

            @Override
            public void onFailure(Call<ResponseData<List<LookMyBillModel>>> call, Throwable t) {
                super.onFailure(call, t);
                isDataError = true;
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "", loadMore);
            }
        });
    }

    @Override
    public void detachView() {
    }

    @Override
    public Object getCacheData() {
        return null;
    }
}
